﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Web;

namespace BZ.Common
{
    /// <summary>
    /// 比较简单的序列化功能
    /// 目前包含XML序列化
    /// </summary>
    public class SerializationUtility
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string AddCDATA(string strInfo)
        {
            if (strInfo != null)
            {
                StringBuilder myStr = new StringBuilder(strInfo);

                //清除掉原有标记
                myStr.Replace("<![CDATA[", HttpUtility.HtmlEncode("<![CDATA[")).Replace("]]>", HttpUtility.HtmlEncode("]]>"));

                //增加标记
                myStr.Insert(0, "<![CDATA[");
                myStr.Append("]]>");

                return myStr.ToString();
            }
            else
            {
                return null;
            }
        }

        public static string RemoveCDATA(string strInfo)
        {
            if (strInfo != null)
            {
                StringBuilder myStr = new StringBuilder(strInfo);

                //清除掉原有标记
                myStr.Replace("<![CDATA[", "").Replace("]]>", "");

                //恢复原有标记
                myStr.Replace(HttpUtility.HtmlEncode("<![CDATA["), "<![CDATA[").Replace(HttpUtility.HtmlEncode("]]>"), "]]>");

                return myStr.ToString();
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获取Xml串(默认utf8编码)
        /// </summary>
        /// <param name="_xmlObject">序列化对象</param>
        /// <param name="_xmlRootName">根节点名</param>
        /// <param name="_xmlRootNamespace">命名空间</param>
        /// <returns></returns>
        public static string ParseToXml(object _xmlObject, string _xmlRootName, string _xmlRootNamespace)
        {
            if (_xmlObject != null && !string.IsNullOrEmpty(_xmlRootName) && !string.IsNullOrEmpty(_xmlRootNamespace))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    StringBuilder myResult = new StringBuilder();
                    myResult.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                    new DataContractSerializer(_xmlObject.GetType(), _xmlRootName, _xmlRootNamespace).WriteObject(ms, _xmlObject);
                    myResult.Append(Encoding.UTF8.GetString(ms.ToArray()));
                    return myResult.ToString();
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 根据Xml串获取对象(默认utf8编码)
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="_xmlString">Xml串</param>
        /// <param name="_xmlRootName">根节点名</param>
        /// <param name="_xmlRootNamespace">命名空间</param>
        /// <returns></returns>
        public static T ParseToObject<T>(string _xmlString, string _xmlRootName, string _xmlRootNamespace)
        {
            if (!string.IsNullOrEmpty(_xmlString) && !string.IsNullOrEmpty(_xmlRootName) && !string.IsNullOrEmpty(_xmlRootNamespace))
            {
                using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(_xmlString)))
                {
                    return (T)new DataContractSerializer(typeof(T), _xmlRootName, _xmlRootNamespace).ReadObject(ms);
                }
            }
            return default(T);
        }
    }
}
